<?php
/**
 * Template for month views.
 * @author Ian Dunn <scholar42@gmail.com>
 * @version 1.0
 * @since 2013-11-20
 */
require_once 'login_info.php';
require_once 'schedule.php';

loginInfo::update_login ();

$month_str = $_GET['month'];

// load month variables.

$file = sprintf ("%s/%s.php", schedule::sched_dir(), $month_str);

include $file;

$mon = 0;
$year = 0;
list ($year, $mon) = sscanf ($month_str, "%d_%d");

$month_name = date("F", mktime(0,0,0,(int)$mon,1,(int)$year));

$next_date = getdate(mktime(0,0,0,(int)$mon+1,1,(int)$year));
$prev_date = getdate(mktime(0,0,0,(int)$mon-1,1,(int)$year));

$next_file = sprintf("%d_%d", (int)$next_date['year'], (int)$next_date['mon']);
$prev_file = sprintf("%d_%d", (int)$prev_date['year'], (int)$prev_date['mon']);

// will set $month_vars 

?>

<html>
	<head>
		<title>Opol - Schedule For <?php print "$mon/$year";?> </title>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php'; ?>
		<br>
		<table border="1" class='sched'>
			<tr>
				<td class='sched_nav'><?php print "<a href='/view_month.php?month=$prev_file'>";?>&lt;</a></td>
				<td class='sched_name' colspan="5"><?php print "$month_name, $year";?></td>
				<td class='sched_nav'><?php print "<a href='/view_month.php?month=$next_file'>";?>&gt;</td>
			</tr>
			<tr>
<?php
	$days_of_week = array ("Sat","Mon","Tues","Wed","Thurs","Fri","Sat");
	foreach ($days_of_week as $name) {
		print "\t\t\t\t<td>$name</td>\n";
	}
?>
			</tr>
<?php
print "\t\t\t<tr>\n";
$proc_time = mktime (0, 0, 0, (int)$mon, 1, (int)$year);
$days = (int)date("t", $proc_time);
$proc_date = getdate ($proc_time);
$wday = (int)$proc_date['wday'];
$day = 1 - $wday;
while ($day < 1) {
	$proc_time = mktime (0, 0, 0, (int)$mon, (int)$day, (int)$year);
	$proc_date = getdate ($proc_time);
	printf("\t\t\t\t<td class='oth_day_num'>%d</td>\n", (int)$proc_date['mday']);
	$day++;
}

if (isset ($GLOBALS['month_vars']))
	$month_vars = $GLOBALS['month_vars'];

for (; $day <= $days; $day++) {
	if (isset ($month_vars) && isset ($month_vars[$day]))
		$day_vars = $month_vars[$day];

	$day_str = sprintf ("%d_%d_%d", $year, $mon, $day);
	print "\t\t\t\t<td class='day_num'>\n";
	print "\t\t\t\t\t<a href='/view_day.php?day=$day_str'>$day</a><br>\n";

	if (isset ($day_vars)) {
		print "\t\t\t\t\t<ul class='phase_list'>\n";
		foreach ($day_vars as $string) {
			print "\t\t\t\t\t\t<li>$string</li>\n";
		}
		unset ($day_vars);
		print "\t\t\t\t\t</ul>\n";
	}
	print "\t\t\t\t</td>\n";

	$wday++;
	if ($wday == 7) {
		print "\t\t\t</tr>\n";
		print "\t\t\t<tr>\n";
		$wday = 0;
	}
}

if ($wday != 0) {
	$day = 1;
	for (; $wday < 7; $wday++, $day++) {
		printf ("\t\t\t\t<td class='oth_day_num'>%d</td>\n", (int)$day);
	}
}

print "\t\t\t</tr>\n";

?>
	  </table>
	</body>
</html>
